import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import home from '../components/home.vue'
import login from '../components/login.vue'
import welcome from '../components/welcome.vue'
import users from '../components/user/users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import List from '../components/goods/List.vue'
import Add from '../components/goods/Add.vue'
import order from '../components/order/order.vue'
Vue.use(VueRouter)

const router = new VueRouter({
    // 路由规则
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: login },
        {
            path: '/home',
            component: home,
            redirect: '/welcome',
            children: [
                { path: '/welcome', component: welcome },
                { path: '/users', component: users },
                { path: '/rights', component: Rights },
                { path: '/roles', component: Roles },
                { path: '/categories', component: Cate },
                { path: '/params', component: Params },
                { path: '/goods', component: List },
                { path: '/goods/add', component: Add },
                { path: '/orders', component: order }
            ]
        }
    ]
});
// 路由导航守卫
router.beforeEach((to, from, next) => {
    // to将要访问的路径
    // from代表从哪个路径跳转而来
    // next是一个函数代表放行   next()放行  next('/login') 强制跳转
    if (to.path == '/login') return next();
    // 如果不访问登陆页面就要验证有没有登陆   获取用geiItem
    const tokenstr = window.sessionStorage.getItem('token');
    // 如果不存在强制去登陆页面
    if (!tokenstr) return next('/login')
    next();
})
export default router